﻿namespace Votations.NSurvey.SQLServerDAL
{
    using System;
    using Votations.NSurvey.Data;
    using Votations.NSurvey.IDAL;
    using Microsoft.ApplicationBlocks.Data;
    using System.Data;
    using System.Data.SqlClient;

    public class Folder : IFolder
    {
        public FolderData GetAllFolders()
        {
            FolderData folderData = new FolderData();

            //SqlHelper.FillDataset(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spFolderGetAll", folderData, new string[] { "Folder" });

            return folderData;
        }


        public void AddFolder(int parentFolderId, string folderName)
        {
            SqlParameter[] spParameters = new SqlParameter[2];
			spParameters[0]	 = new SqlParameter("@ParentFolderId", parentFolderId);            
            spParameters[1]	 = new SqlParameter("@FolderName", SqlDbType.VarChar, 200, folderName);
            spParameters[1].Value = folderName;

            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, "vts_spFolderAddNew", spParameters);
            
        }

        public void UpdateFolder(int ?parentFolderId, int folderId, string folderName)
        {
            SqlParameter[] spParameters = new SqlParameter[3];
            spParameters[0] = new SqlParameter("@FolderId", folderId);
            spParameters[1] = new SqlParameter("@FolderName", SqlDbType.VarChar, 200);
            spParameters[1].Value = folderName;
            spParameters[2] = new SqlParameter("@ParentFolderId", parentFolderId);
			
            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, "vts_spFolderUpdate", spParameters);
        }

        public void DeleteFolderById(int id)
        {            
            SqlParameter[] spParameters = new SqlParameter[1];
			spParameters[0]	 = new SqlParameter("@FolderId", id);
			
            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, "vts_spFolderDelete", spParameters);
        }

        public FolderData GetTreeNodes(int UserId)
        {
            FolderData folderData = new FolderData();
            SqlParameter[] spParameters = new SqlParameter[1];
            spParameters[0] = new SqlParameter("@UserId", UserId);
            SqlHelper.FillDataset(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spTreeNodesGetAll", folderData, new string[] { "TreeNodes" },spParameters);

            return folderData;
        }


        public void MoveFolder(int? parentFolderId, int folderId)
        {
            SqlParameter[] spParameters = new SqlParameter[2];
            spParameters[0] = new SqlParameter("@ParentFolderId", parentFolderId);
            spParameters[1] = new SqlParameter("@FolderId", folderId);

            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, "vts_spFolderMove", spParameters);            
        }

        public FolderData GetFolderById(int folderId)
        {
            FolderData folderData = new FolderData();
            SqlParameter[] spParameters = new SqlParameter[1];
            spParameters[0] = new SqlParameter("@FolderId", folderId);

            SqlHelper.FillDataset(SqlHelper.DbConnectionString, CommandType.StoredProcedure, 
                "vts_spFolderGetByFolderId", folderData, new string[] { "TreeNodes" }, spParameters);

            return folderData;
        }
    }
}
